package com.zpark.drug.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpark.drug.common.ResultMapUtil;
import com.zpark.drug.pojo.PurchaseRecords;
import com.zpark.drug.service.PurchaseRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/purchaseRecords")
public class PurchaseRecordsController {
    @Autowired
    private PurchaseRecordsService purchaseRecordsService;

    @RequestMapping
    public String login(){
        return "/purchaseRecords";
    }
    /**
     * 转向供应商新增页面
     * @return
     */
    @RequestMapping(value = "/purchaseRecordsPage")
    public String lossPage(){
        return "/purchaseRecordsPage";
    }
    /**
     * 转向供应商编辑页面
     * @return
     */
    @RequestMapping(value = "/purchaseRecordsQueryById")
    public Object purchaseRecordsQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        PurchaseRecords purchaseRecords = purchaseRecordsService.findById(id);
        model.addAttribute("obj",purchaseRecords);
        return "/purchaseRecordsPage";
    }
    /**
     * 获取所有供应商
     * @return
     */
    @RequestMapping(value = "/purchaseRecordsList")
    @ResponseBody
    public Object purchaseRecordsList(){
        List<PurchaseRecords> purchaseRecordsList = purchaseRecordsService.findAll();
        return ResultMapUtil.getHashMapList(purchaseRecordsList);
    }

    /**
     * 添加一个供应商
     * @return
     */
    @RequestMapping(value = "/purchaseRecordsAdd")
    @ResponseBody
    public Object purchaseRecordsAdd(PurchaseRecords purchaseRecords){
        try {
            int i = purchaseRecordsService.addPurchaseRecords(purchaseRecords);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/purchaseRecordsEdit")
    @ResponseBody
    public Object purchaseRecordsEdit(PurchaseRecords purchaseRecords){
        try {
            int i = purchaseRecordsService.updatePurchaseRecords(purchaseRecords);

            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个供应商
     * @return
     */
    @RequestMapping(value = "/purchaseRecordsDelById")
    @ResponseBody
    public Object purchaseRecordsDelById(Integer id){
        try {
            int i = purchaseRecordsService.deletePurchaseRecordsById(id);

            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }
    /**
     * 分页查询供应商列表
     * @return
     */
    @RequestMapping(value = "/purchaseRecordsQueryPage")
    @ResponseBody
    public Map<String, Object> purchaseRecordsQueryPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String param) {
        PageInfo<PurchaseRecords> purchaseRecordsPageInfo = purchaseRecordsService.findPage(page, limit, param);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询数据成功");
        map.put("count", purchaseRecordsPageInfo.getTotal());
        map.put("data", purchaseRecordsPageInfo.getList());
        return map;
    }

}
